home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- import os
- import sys
- import types
- import inspect
- from StringIO import StringIO
- from zope.interface import classProvides, implements, Interface
- from ZSI import _get_element_nsuri_name, EvaluateException, ParseException, fault, ParsedSoap, SoapWriter
- from ZSI.twisted.reverse import DataHandler, ReverseHandlerChain, HandlerChainInterface
-
- def soapmethod(requesttypecode, responsetypecode, soapaction = '', operation = None, **kw):
-
- def _closure(func_cb):
- func_cb.root = (requesttypecode.nspname, requesttypecode.pname)
- func_cb.action = soapaction
- func_cb.requesttypecode = requesttypecode
- func_cb.responsetypecode = responsetypecode
- func_cb.soapmethod = True
- func_cb.operation = None
- return func_cb
-
- return _closure
-
-
- class SOAPCallbackHandler:
- classProvides(HandlerChainInterface)
- writerClass = None
-
- def processRequest(cls, ps, **kw):
- resource = kw['resource']
- request = kw['request']
- root = _get_element_nsuri_name(ps.body_root)
- for key, method in inspect.getmembers(resource, inspect.ismethod):
- if getattr(method, 'soapmethod', False) and method.root == root:
- break
- continue
- else:
- raise RuntimeError, 'Missing soap callback method for root "%s"' % root
-
- try:
- req = ps.Parse(method.requesttypecode)
- except Exception:
- ex = None
- raise
-
-
- try:
- rsp = method.responsetypecode.pyclass()
- except Exception:
- ex = None
- raise
-
-
- try:
- (req, rsp) = method(req, rsp)
- except Exception:
- ex = None
- raise
-
- return rsp
-
- processRequest = classmethod(processRequest)
-
- def processResponse(cls, output, **kw):
- sw = SoapWriter(outputclass = cls.writerClass)
- sw.serialize(output)
- return sw
-
- processResponse = classmethod(processResponse)
-
-
- class SOAPHandlerChainFactory:
- protocol = ReverseHandlerChain
-
- def newInstance(cls):
- return cls.protocol(DataHandler, SOAPCallbackHandler)
-
- newInstance = classmethod(newInstance)
-
-
- class WSGIApplication(dict):
- encoding = 'UTF-8'
-
- def __call__(self, env, start_response):
- script = env['SCRIPT_NAME']
- ipath = os.path.split(env['PATH_INFO'])[1:]
- for i in range(1, len(ipath) + 1):
- path = os.path.join(*ipath[:i])
- print 'PATH: ', path
- application = self.get(path)
- if application is not None:
- env['SCRIPT_NAME'] = script + path
- env['PATH_INFO'] = ''
- print 'SCRIPT: ', env['SCRIPT_NAME']
- return application(env, start_response)
- continue
-
- return self._request_cb(env, start_response)
-
-
- def _request_cb(self, env, start_response):
- start_response('404 ERROR', [
- ('Content-Type', 'text/plain')])
- return [
- 'Move along people, there is nothing to see to hear']
-
-
- def putChild(self, path, resource):
- path = path.split('/')
- lp = len(path)
- if lp == 0:
- raise RuntimeError, 'bad path "%s"' % path
-
- if lp == 1:
- self[path[0]] = resource
-
- for i in range(len(path)):
- if not path[i]:
- continue
-
-
- next = self.get(path[i], None)
- if next is None:
- next = self[path[i]] = WSGIApplication()
-
- next.putChild('/'.join(path[-1:]), resource)
-
-
-
- class SOAPApplication(WSGIApplication):
- factory = SOAPHandlerChainFactory
-
- def __init__(self, **kw):
- dict.__init__(self, **kw)
- self.delegate = None
-
-
- def _request_cb(self, env, start_response):
- if env['REQUEST_METHOD'] == 'GET':
- return self._handle_GET(env, start_response)
-
- if env['REQUEST_METHOD'] == 'POST':
- return self._handle_POST(env, start_response)
-
- start_response('500 ERROR', [
- ('Content-Type', 'text/plain')])
- s = StringIO()
- h = env.items()
- h.sort()
- for k, v in h:
- print >>s, k, '=', `v`
-
- return [
- s.getvalue()]
-
-
- def _handle_GET(self, env, start_response):
- if env['QUERY_STRING'].lower() == 'wsdl':
- start_response('200 OK', [
- ('Content-Type', 'text/plain')])
- if not self.delegate:
- pass
- r = self
- return _resourceToWSDL(r)
-
- start_response('404 ERROR', [
- ('Content-Type', 'text/plain')])
- return [
- 'NO RESOURCE FOR GET']
-
-
- def _handle_POST(self, env, start_response):
- input = env['wsgi.input']
- data = input.read(int(env['CONTENT_LENGTH']))
- mimeType = 'text/xml'
- if self.encoding is not None:
- mimeType = 'text/xml; charset="%s"' % self.encoding
-
- request = None
- if not self.delegate:
- pass
- resource = self
- chain = self.factory.newInstance()
-
- try:
- pyobj = chain.processRequest(data, request = request, resource = resource)
- except Exception:
- ex = None
- start_response('500 ERROR', [
- ('Content-Type', mimeType)])
- return [
- fault.FaultFromException(ex, False, sys.exc_info()[2]).AsSOAP()]
-
-
- try:
- soap = chain.processResponse(pyobj, request = request, resource = resource)
- except Exception:
- ex = None
- start_response('500 ERROR', [
- ('Content-Type', mimeType)])
- return [
- fault.FaultFromException(ex, False, sys.exc_info()[2]).AsSOAP()]
-
- start_response('200 OK', [
- ('Content-Type', mimeType)])
- return [
- soap]
-
-
-
- def test(app, port = 8080, host = 'localhost'):
- reactor = reactor
- import twisted.internet
- log = log
- import twisted.python
- HTTPFactory = HTTPFactory
- import twisted.web2.channel
- Site = Site
- import twisted.web2.server
- WSGIResource = WSGIResource
- import twisted.web2.wsgi
- log.startLogging(sys.stdout)
- reactor.listenTCP(port, HTTPFactory(Site(WSGIResource(app))), interface = host)
- reactor.run()
-
-
- def _issoapmethod(f):
- if type(f) is types.MethodType:
- pass
- return getattr(f, 'soapmethod', False)
-
-
- def _resourceToWSDL(resource):
- ElementTree = ElementTree
- import xml.etree
- Element = Element
- QName = QName
- import xml.etree.ElementTree
- WSDL = WSDL
- import ZSI.wstools.Namespaces
- r = resource
- methods = filter(_issoapmethod, (map,)((lambda i: getattr(r, i)), dir(r)))
- tns = ''
- defs = Element('{%s}definitions' % WSDL.BASE)
- defs.attrib['name'] = 'SampleDefs'
- defs.attrib['targetNamespace'] = tns
- porttype = Element('{%s}portType' % WSDL)
- porttype.attrib['name'] = QName('{%s}SamplePortType' % tns)
- binding = Element('{%s}binding' % WSDL)
- defs.append(binding)
- binding.attrib['name'] = QName('{%s}SampleBinding' % tns)
- binding.attrib['type'] = porttype.get('name')
- for m in methods:
- m.action
-
- service = Element('{%s}service' % WSDL.BASE)
- defs.append(service)
- service.attrib['name'] = 'SampleService'
- port = Element('{%s}port' % WSDL.BASE)
- service.append(port)
- port.attrib['name'] = 'SamplePort'
- port.attrib['binding'] = binding.get('name')
- soapaddress = Element('{%s}address' % WSDL.BIND_SOAP)
- soapaddress.attrib['location'] = 'http://localhost/bla'
- port.append(soapaddress)
- return [
- ElementTree.tostring(defs)]
-
-